//
//  ROFoodDetail.swift
//  NeusoftSchool
//
//  Created by 张瑞 on 2017/4/12.
//  Copyright © 2017年 李狗蛋. All rights reserved.
//

import UIKit

class ROFoodDetail: UITableViewController {
    

    var food: ROFoodModel!
    var commentList: NSArray = NSArray()

    override func viewDidLoad() {
        super.viewDidLoad()
       
        self.title = food.foodname as String

        NotificationCenter.default.addObserver(self, selector:#selector(ROFoodDetail.makeOrder),name:NSNotification.Name(rawValue: "Order"), object: nil)
        let paraDict = ["food_id":food.food_id!] as NSDictionary
        RONetworkMngTool.sharedNetwodkMngTool().RONetwork_GetFoodCommentListParameters(paraDict, view: self.view) { (commentArray) in
            self.commentList = commentArray
            self.tableView.reloadData()
        }
    }
    func makeOrder(){
        self.performSegue(withIdentifier: "toOrder", sender: food)
        
    }
    override func prepare(for segue:UIStoryboardSegue, sender: Any?) {
        let orderController: ROOrder = segue.destination as!  ROOrder
        //构建传递food模型和shop_id的dictionary
        orderController.food = sender as! ROFoodModel
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if section == 0 {
            return 3
        }else {
            return self.commentList.count
        }
        
    }
    
//    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return 100
//    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var basecell: UITableViewCell = UITableViewCell()
        if indexPath.section == 0 {
            if(indexPath as NSIndexPath).row == 0 {
            let cell : FoodHeadCell = tableView.dequeueReusableCell(withIdentifier: "FoodHeadCell", for: indexPath) as! FoodHeadCell
            cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CELLHEIGHT);
            
            tableView.separatorStyle = UITableViewCellSeparatorStyle.none;
            
            //let imageStr = (food.value(forKey: "food") as! ROFoodModel).pic
            let imageStr = food.pic
            let imageURL = URL(string: imageStr! as String)
                if food.pic != "" {
                    cell.foodImageView!.setImageWith(imageURL!, placeholderImage:UIImage(named: "imageViewHolder"))
                } else {
                    cell.foodImageView.image = UIImage(named: "imageViewHolder")
                }
            
            
            //菜品名称
            cell.foodName.text = "\(food.foodname!)"
            //菜品价格
            cell.price.text = "\(food.price!)"
            //推荐指数
            cell.recommand.text = "\(food.recommand!)"
            //商店id
            cell.shop_id.text = "\(food.shop_id!)"
            print(food.intro)
            basecell = cell
        } else if(indexPath as NSIndexPath).row == 1{
            let cell: FoodJsCell = tableView.dequeueReusableCell(withIdentifier: "FoodJsCell", for: indexPath) as! FoodJsCell
            cell.food = food;
            cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: FoodJsCell.cellHeightWithPostinModel(food));
            basecell = cell
        } else {
            let cell = FoodDetailCell.creatCell(tableView)
            cell.food = food
            basecell = cell
            }
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "foodCommentCell", for: indexPath) as! FoodCommentCell
            cell.selectionStyle = .none
            if commentList.count == 0{
                cell.textLabel?.text = "无评价"
            }else {
                let dict = commentList[indexPath.row] as! ROCommentModel
                cell.textLabel?.text = dict.content as String
                cell.foodName.text = dict.foodname as String
                cell.price.text = "单价:\(dict.price as Int)元"
                cell.shopName.text = dict.shopname as String
                cell.userName.text = "\(dict.username as String)--\(dict.comment_time as String)"
            }
            basecell = cell
        }
        
        return basecell
    }
    
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if ((indexPath as NSIndexPath).row == 0) {
            return CELLHEIGHT;
        }else if((indexPath as NSIndexPath).row == 1){
            //返回可变的cell高度 需要根据模型的内容进行计算
            // return ROCourseIntroduceCell.cellHeightWithPostinModel(food)
            return CELLHEIGHT;
            
        }else{
            return 60;
        }
        
        
    }
    //
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 {
            return 40
        }else {
            return 0
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 1 {
            let headerView = UIView()
            headerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 40)
            headerView.backgroundColor = UIColor.green
            let titleLabel = UILabel()
            titleLabel.text = "菜品评论" as String
            titleLabel.textColor = UIColor.black
            titleLabel.font = UIFont.systemFont(ofSize: 12)
            titleLabel.sizeToFit()
            titleLabel.center = CGPoint(x: self.view.frame.width/2, y: 20)
            headerView.addSubview(titleLabel)
            return headerView
        }else {
            return nil
        }
    }

    
}
